package com.ghyf.mplay.bean;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.ghyf.mplay.datatype.AlignMode;
import com.ghyf.mplay.datatype.BackParam;
import com.ghyf.mplay.datatype.FontParam;
import com.ghyf.mplay.datatype.GradientColorType;
import com.ghyf.mplay.datatype.POS;
import com.ghyf.mplay.datatype.RGBA;
import com.ghyf.mplay.datatype.ShowType;
import com.ghyf.mplay.net.data.PriorityTextData;
import com.ghyf.mplay.util.LogUtil;
import com.ghyf.mplay.util.TypeFaceFactory;
import com.ghyf.mplay.view.MExigent;

/**
 * 显示紧急消息模块的信息类
 * @author 陈小红
 *
 */
public class ExigentInfo extends BaseModuleInfo {
	private static final String TAG="ExigentInfo";
	/*
	 * 文件版本 File_Version CHAR 1 模块名称 Module_Name CHAR Exigent1 UID Module_UID
	 * INT GID Module_GIU INT 模块位置 Mudule_Pos POS 全屏 背景参数 Backgroud BACK_PARAM
	 * 见§3.1.4节 - 对齐方式 Align ENUM 5 左边距 Left_Margin INT 0 上边距 Top_Margin INT 0
	 * 字体 Font FONT_PARAM 见§3.1.2节 宋体40号 显示方式 Show_Type ENUM 左滚/上滚/静态 0 滚动速度
	 * Scroll_Speed INT 30 最小字号 Min_Size INT 60
	 */

	// <Backgroud Type="3" PureColor="(0,0,0,255)" GradientColor1="(0,0,0,255)"
	// GradientColor2="(0,0,0,255)" BKFile="" GradientColorType="0"/>
	private BackParam backgroud;// 背景信息

	private Context mContext=null;
	
	// <Align>5</Align>
	private AlignMode align;// 对齐方式

	// <Left_Margin>0</Left_Margin>
	private int left_margin;

	// <Top_Margin>0</Top_Margin>
	private int top_margin;

	/*
	 * <Font Name="宋体" Size="70" Width="0" Escapement="0" Orientation="0"
	 * Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	 * FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	 * ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	 */
	private FontParam font;// 字体信息

	// <Show_Type>2</Show_Type>
	private ShowType show_type;// 显示方式

	// <Scroll_Speed>25</Scroll_Speed>
	private int scroll_speed;// 滚动速度

	// <Min_Size >50</Min_Size>
	private int min_size;// 最小字号。

	private String text;
	private MExigent mExigent;

	@Override
	public MExigent getView(Context context) {
		mContext=context;
		 mExigent = new MExigent(context);

		
		//设置控件基本属性
		mExigent.setModule_type(module_type);
		mExigent.setzOrder(zOrder);
		mExigent.setFile_version(file_version);
		mExigent.setModule_name(module_name);
		mExigent.setModule_uid(module_uid);
		mExigent.setModule_gid(module_gid);
		
		//  显示方式
//		 mExigent.setTextScaleX(0.5f);

		// 位置
		initPosition(mExigent, pos);

//		 背景
		initBackground(mExigent, backgroud);

		// 设置文本及其字体
		//本站发生紧急事件,请各位乘客不要慌张,听从工作人员指引,从B出口顺序离站,谢谢!
		showText(mExigent, font, "");

		//对齐方式实现九宫格中左上，左下，右上，右下对齐方式。
		setGravity();

		// 设置左、上的padding（用left、top margin变量设置）
//		mExigent.setPadding(left_margin, top_margin, 0, 0);
		mExigent.setPadding(0, 0, 0, 0);
	
		return mExigent;
	}
	/**
	 * 对齐方式实现九宫格中左上，左下，右上，右下对齐方式<br>
	 * 2014-4-30 下午2:23:54
	 *
	 */
	void setGravity(){
		switch (align) {
		case LOWLEFT:
			mExigent.setGravity(Gravity.LEFT|Gravity.BOTTOM);
			break;
		case LOWMIDDLE:
			mExigent.setGravity(Gravity.CENTER|Gravity.BOTTOM);
			break;
		case LOWRIGHT:
			mExigent.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
			break;
		case MIDDLELEFT:
			mExigent.setGravity(Gravity.LEFT|Gravity.CENTER);
			break;
		case CENTER:
			mExigent.setGravity(Gravity.CENTER);
			break;
		case MIDDLERIGHT:
			mExigent.setGravity(Gravity.RIGHT|Gravity.CENTER);
			break;
		case UPLEFT:
			mExigent.setGravity(Gravity.LEFT|Gravity.TOP);
			break;
		case UPMIDDLE:
			mExigent.setGravity(Gravity.CENTER|Gravity.TOP);
			break;
		case UPRIGHT:
			mExigent.setGravity(Gravity.RIGHT|Gravity.TOP);
			break;
		default:
			break;
		}
	}
	/**
	 * 设置当前View显示/隐藏
	 * 2014-5-21 下午3:25:01
	 * @param isShow
	 *
	 */
	public void setVisibility(boolean isShow){
		if(isShow){
			mExigent.setVisibility(View.VISIBLE);
		}else{
			mExigent.setVisibility(View.GONE);
		}
	}
	// <Module_Pos>(0,0,720,576)</Module_Pos>
	// <Backgroud Type="3" PureColor="(0,0,0,255)" GradientColor1="(0,0,0,255)"
	// GradientColor2="(0,0,0,255)" BKFile="" GradientColorType="0"/>
	// <Align>5</Align>
	// <Left_Margin>0</Left_Margin>
	// <Top_Margin>0</Top_Margin>
	// <Font Name="宋体" Size="70" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	// <Show_Type>2</Show_Type>
	// <Scroll_Speed>25</Scroll_Speed>
	// <Min_Size >50</Min_Size>

	/**
	 * 设置TextView的文本和字体
	 * 
	 * @param tv
	 *            textview控件
	 * @param font
	 *            文本的字体
	 * @param text
	 *            文本
	 */
	@SuppressLint("ResourceAsColor")
	void showText(TextView tv, FontParam font, String text) {
		tv.setText(text);
		// 大小font.getSize()
		tv.setTextSize(font.getSize());
		// 字体
		Typeface typeface = TypeFaceFactory.createTypeface(font.getName());
		tv.setTypeface(typeface);
		// 颜色
		RGBA rgba = font.getFaceColor();
		int color = Color.argb(rgba.getAlpha(), rgba.getRed(), rgba.getGreen(),
				rgba.getBlue());
		tv.setTextColor(color);
//		tv.setTextColor(Color.YELLOW);
//		mExigent.setBackgroundColor(Color.BLACK);
//		tv.setTextColor(R.color.zise);
	}
	
	public void setTextSize(float size){
		mExigent.setTextSize(size);
	}

	/**
	 * 初始化控件的背景
	 * 
	 * @param viwe
	 *            被设置的控件
	 * @param bg
	 *            控件的背景
	 */
	void initBackground(View view, BackParam bg) {
		switch (bg.getType()) {
		case NOTSHOW: // 背景不显示
			break;
		case GRADIENTCOLOR: // 背景类型为渐变色
			if (bg.getColorType() == GradientColorType.HORIZONTALGRADIENT) {
				//水平渐变 参考：url:http://blog.csdn.net/a_large_swan/article/details/7107126
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			} else {
				// 垂直渐变
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			}
			break;
		case PICTURE: // 背景类型为图片
			// 本地图片文件路径生成drawable
			Bitmap bitmap=null;
			bitmap= BitmapFactory.decodeFile(bg.getBkfile().getPath());
			Drawable drawable = new BitmapDrawable(mContext.getResources(),bitmap);
			// 设置图片背景
			view.setBackgroundDrawable(drawable);
			
			
			break;
		case PURECOLOR: // 背景类型为纯色
			RGBA purecolor = bg.getPurecolor();
			view.setBackgroundColor(Color.argb(purecolor.getAlpha(),
					purecolor.getRed(), purecolor.getGreen(),
					purecolor.getBlue()));
			break;
		default: // 其他
			break;
		}
	}
	
	/**
	 * 接收发送的紧急模板的文件指令
	 * 2014-7-23 下午3:07:50
	 * @param bundle
	 *
	 */
	public void receiveControlCmd(PriorityTextData data){
//		PriorityTextData data=(PriorityTextData)bundle.getSerializable(ConstantValue.EXTRA_OBJ);
		text=data.getTextContent();
		LogUtil.i(TAG, "接收:"+text);
		showText(mExigent, font, text);
	}
	
	
	


	
	/**
	 * 初始化控件的位置
	 * 
	 * @param view
	 *            被设置的控件
	 * @param pos
	 *            控件的位置
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	void initPosition(View view, POS pos) {
		view.setX(pos.getLeft());
		view.setY(pos.getTop());
		view.setLayoutParams(new LayoutParams(pos.getWidth(), pos.getHeight()));
	}


	public BackParam getBackgroud() {
		return backgroud;
	}

	public void setBackgroud(BackParam backgroud) {
		this.backgroud = backgroud;
	}

	public AlignMode getAlign() {
		return align;
	}

	public void setAlign(AlignMode align) {
		this.align = align;
	}

	public int getLeft_margin() {
		return left_margin;
	}

	public void setLeft_margin(int left_margin) {
		this.left_margin = left_margin;
	}

	public int getTop_margin() {
		return top_margin;
	}

	public void setTop_margin(int top_margin) {
		this.top_margin = top_margin;
	}

	public FontParam getFont() {
		return font;
	}

	public void setFont(FontParam font) {
		this.font = font;
	}

	public ShowType getShow_type() {
		return show_type;
	}

	public void setShow_type(ShowType show_type) {
		this.show_type = show_type;
	}

	public int getScroll_speed() {
		return scroll_speed;
	}

	public void setScroll_speed(int scroll_speed) {
		this.scroll_speed = scroll_speed;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getMin_size() {
		return min_size;
	}

	public void setMin_size(int min_size) {
		this.min_size = min_size;
	}

}
